#!/usr/bin/env python

# -*- coding: utf-8 -*-

"""
	Copyright (C) 2008  Michael Ughetto <telimektar1er@gmail.com>

	This file is part of Danuve.
	Danuve is a easy to use numerical tool meant to solve chain of oscillators

	Danuve is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	Danuve is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with Danuve.  If not, see <http://www.gnu.org/licenses/>.
"""  

import sys

def section_check(conf_file, required_sections):
	"""section check the presence of required sections
	
	Keyword arguments :
	conf_file -- a RawConfigParser instance fullfilled with a valid read method
	required_sections -- list containing required sections (name are 
	case-sensitive)
	
	"""
	require_errors = []
	print "Checking required sections..."
	for section in required_sections:
		if conf_file.has_section(section):
			pass
			#print section, "\t[ OK ]"
		else :
			print section, "\t[ FAILED ]"
			require_errors.append(section)
	if len(require_errors) != 0:
		print "Please add missing section(s) to your *.ini file"
		sys.exit(1)
	else :
		print "Required sections found\n"

def option_check(conf_file, section, required_options):
	"""option_check check the state of different required options

	Keyword arguments :
	conf_file -- a RawConfigParser instance fullfilled with a valid read method
	section -- The section to inspect
	required_options -- a list of expected options in section
	
	"""
	require_errors = []
	print "Checking " + section + " options ..."
	for option in required_options:
		if conf_file.has_option(section, option):
			if len(conf_file.get(section, option)) == 0:
				print option, "\t[ EMPTY ]"
				require_errors.append(option)
			else:
				pass
				#print option, "\t[ OK ]"
		else:
			print option, "\t[ FAILED ]"
			require_errors.append(section)
	if len(require_errors) != 0:
			print "Please add missing option(s) to your *.ini file"
			sys.exit(1)
	else:
		print "[ OK ]"

def check_struct(structure):
	""" check_struct verify the validity of the Structure option

	structure -- structure string exctract from the configuration file

	"""
	if structure[0] == '|':
		i = 1
		while structure[i:i+3] == '--O':
			i=i+3
		if i == len(structure):
			pass
		elif structure[i:i+3] == '--|':
			pass
		else:
			print "Illegal structure, please refer to documentation"
			sys.exit(1)
	else:
		print "Illegal structure, please refer to documentation"
		sys.exit(1)

def get_list(conf_file, section, option):
	""" get_list is used to get and control an option that should be a list

	Keyword arguments :
	conf_file -- a RawConfigParser instance fullfilled with a valid read method
	section -- The section where the option lies
	option -- the option to get and control

	"""
	data = eval(conf_file.get(section, option))
	if type(data) != list:
		print option, "MUST be a list"
	return data

def get_data(conf_file, section, option):
	""" get_data is used to get and control an option that should be an int or a
	float

	Keyword arguments :
	conf_file -- a RawConfigParser instance fullfilled with a valid read method
	section -- The section where the option lies
	option -- the option to get and control

	"""
	data = eval(conf_file.get(section, option))
	if type(data) != int :
		if type(data) != float:
			print option, "MUST be an integer or a float"
	return data

def get_spring(conf_file, structure):
	""" get_spring is used to build two lists useful for the physic solution

	Keyword arguments :
	conf_file -- a RawConfigParser instance fullfilled with a valid read method
	structure -- the string that describes the system

	Returned lists :
	left_spring -- this is the list of spring found on the left of masses
	right_spring -- this is the list of spring found on the right of masses

	"""
	spring = eval(conf_file.get('Carac','carac_springs'))
	left_spring = list(spring)
	right_spring = list(spring)
	if structure[len(structure)-3:len(structure)] == '--|':
		left_spring.pop()
		right_spring.pop(0)
	else:
		right_spring.append(0)
	return left_spring, right_spring
